#include<iostream>
#include <opencv4/opencv2/opencv.hpp>
#include <vector>

using namespace std;
using namespace cv;


class QrCode
{
public:
    QrCode(int port):cap(port){
        cap.set(CAP_PROP_FRAME_WIDTH,this->cameraWidth);
	    cap.set(CAP_PROP_FRAME_HEIGHT,this->cameraHeigh);
    };
    ~QrCode(){};
    void setWidthAndHeigh(int width,int heigh){
        this->cameraWidth=width;
        this->cameraHeigh=heigh;
        cap.set(CAP_PROP_FRAME_WIDTH,this->cameraWidth);
	    cap.set(CAP_PROP_FRAME_HEIGHT,this->cameraHeigh);
    }
    void discren(bool k){
        this->cap>>this->display;
        if(k){
            scan();
        }
        imshow("qr_display", this->display);
        waitKey(5);
    }
private:
    Mat display;
	VideoCapture cap;
	string information;
    int cameraWidth=1280;
    int cameraHeigh=720;
    void scan();
};

void QrCode::scan(){
    Mat gray,qrcode_bin,img,show;
    QRCodeDetector qrcodedetector;
    vector<Point> points1,points2,points3,points4;
    int i,j,h;
    bool isQRcode=0;
    cap >> show >>img;
    img.setTo(0);
    for(i=0;isQRcode!=1&&i<cameraHeigh;i+=10){
        for(int z=0;z<10;z++){
            for(j=0;j<cameraHeigh&&isQRcode!=1;j++){
                img.at<Vec3b>(j,i+z) = show.at<Vec3b>(j,i+z);
            }
        }
        cvtColor(img, gray, COLOR_BGR2GRAY);
        isQRcode = qrcodedetector.detect(gray, points1);
        // cout<<show.size()<<endl;
        // imshow("resul", img);
    }
    for(int z=0;isQRcode==0&&z<10;z++){
        isQRcode = qrcodedetector.detect(gray, points1);
    }
    if(isQRcode){
        rectangle(img,Point(points1[0]),Point(points1[2]),Scalar(0,0,255),-1);
        information = qrcodedetector.decode(gray, points1, qrcode_bin);
        for (int i = 0; i < points1.size(); i++)//绘制二维码的边框
        {
            if (i == points1.size() - 1)
            {
                line(this->display, points1[i], points1[0], Scalar(0, 0, 255), 2, 8);
                break;
            }
            line(this->display, points1[i], points1[i + 1], Scalar(0, 0, 255), 2, 8);
        }
        putText(this->display, information.c_str(), Point((points1[0]+points1[2])/2), 0, 1.0, Scalar(0, 0, 255), 2, 8);
        cout << "Scan QR:1" << endl;
        // imshow("this->display", this->display);
    }




    h=i;
    isQRcode=0;
    for(i=0;isQRcode!=1&&i<cameraHeigh/4*3;i+=10){
        for(int z=0;z<10;z++){
            for(j=h;j<cameraWidth&&isQRcode!=1;j++){
                img.at<Vec3b>(i+z,j) = show.at<Vec3b>(i+z,j);
            }
        }
        cvtColor(img, gray, COLOR_BGR2GRAY);
        isQRcode = qrcodedetector.detect(gray, points2);
        // imshow("resul", img);
    }
    for(int z=0;isQRcode==0&&z<10;z++){
        isQRcode = qrcodedetector.detect(gray, points2);
    }
    if(isQRcode){
        rectangle(img,Point(points2[0]),Point(points2[2]),Scalar(0,0,255),-1);
        information = qrcodedetector.decode(gray, points2, qrcode_bin);
        for (int i = 0; i < points2.size(); i++)//绘制二维码的边框
        {
            if (i == points2.size() - 1)
            {
                line(this->display, points2[i], points2[0], Scalar(0, 0, 255), 2, 8);
                break;
            }
            line(this->display, points2[i], points2[i + 1], Scalar(0, 0, 255), 2, 8);
        }
        putText(this->display, information.c_str(), Point((points2[0]+points2[2])/2), 0, 1.0, Scalar(0, 0, 255), 2, 8);
        cout << "Scan QR:2" << endl;
        // imshow("display", this->display);
    }



    isQRcode=0;
    for(i;isQRcode!=1&&i<cameraHeigh-10;i+=10){
        for(int z=0;z<10;z++){
            for(j=h;j<cameraWidth&&isQRcode!=1;j++){
                img.at<Vec3b>(i+z,j) = show.at<Vec3b>(i+z,j);
            }
        }
        cvtColor(img, gray, COLOR_BGR2GRAY);
        isQRcode = qrcodedetector.detect(gray, points3);
        // imshow("resul", img);
    }
    for(int z=0;isQRcode==0&&z<10;z++){
        isQRcode = qrcodedetector.detect(gray, points3);
    }
    if(isQRcode){
        rectangle(img,Point(points3[0]),Point(points3[2]),Scalar(0,0,255),-1);
        information = qrcodedetector.decode(gray, points3, qrcode_bin);
        for (int i = 0; i < points3.size(); i++)//绘制二维码的边框
        {
            if (i == points3.size() - 1)
            {
                line(this->display, points3[i], points3[0], Scalar(0, 0, 255), 2, 8);
                break;
            }
            line(this->display, points3[i], points3[i + 1], Scalar(0, 0, 255), 2, 8);
        }
        putText(this->display, information.c_str(), Point((points3[0]+points3[2])/2), 0, 1.0, Scalar(0, 0, 255), 2, 8);
        cout << "Scan QR:3" << endl;
        // imshow("display", this->display);
    }


    isQRcode=0;
    for(i;isQRcode!=1&&i<cameraHeigh-10;i+=10){
        for(int z=0;z<10;z++){
            for(j=h;j<cameraWidth&&isQRcode!=1;j++){
                img.at<Vec3b>(i+z,j) = show.at<Vec3b>(i+z,j);
            }
        }
        cvtColor(img, gray, COLOR_BGR2GRAY);
        isQRcode = qrcodedetector.detect(gray, points4);
        // imshow("resul", img);
    }
    for(int z=0;isQRcode==0&&z<10;z++){
        isQRcode = qrcodedetector.detect(gray, points4);
    }
    if(isQRcode){
        information = qrcodedetector.decode(gray, points4, qrcode_bin);
        for (int i = 0; i < points4.size(); i++)//绘制二维码的边框
        {
            if (i == points4.size() - 1)
            {
                line(this->display, points4[i], points4[0], Scalar(0, 0, 255), 2, 8);
                break;
            }
            line(this->display, points4[i], points4[i + 1], Scalar(0, 0, 255), 2, 8);
        }
        putText(this->display, information.c_str(), Point((points4[0]+points4[2])/2), 0, 1.0, Scalar(0, 0, 255), 2, 8);
        cout << "Scan QR:4" << endl;
        // imshow("display", this->display);
    }
}






